from math import sin, cos, pi
from Campo import *

import pygame, sys, os

pygame.display.set_caption("BADmine - C.J.Production")
displayWidth = 640
displayHeight = 480
fpsLimit = 90

altura = 8
largura = 8
bombas = 10
altura_tela = 500
largura_tela = 260
com_som = 0

def sinInterpolation(start, end, steps=30):
    values = [start]
    delta = end - start
    for i in range(1, steps):
        n = (pi / 2.0) * (i / float(steps - 1))
        values.append(start + delta * sin(n))
    return values

class RotatingMenu:
    def __init__(self, x, y, radius, arc=pi*2, defaultAngle=0, wrap=False):
        self.x = x
        self.y = y
        self.radius = radius
        self.arc = arc
        self.defaultAngle = defaultAngle
        self.wrap = wrap
        
        self.rotation = 0
        self.rotationTarget = 0
        self.rotationSteps = [] #Used for interpolation
        
        self.items = []
        self.selectedItem = None
        self.selectedItemNumber = 0
    
    def addItem(self, item):
        self.items.append(item)
        if len(self.items) == 1:
            self.selectedItem = item
    
    def selectItem(self, number):
        itemNumber = 0 + number
        if itemNumber in [0, 1, 2, 3]:
            self.selectedItem.deselect()
            self.selectedItem = self.items[itemNumber]
            self.selectedItem.select()
            
            self.selectedItemNumber = itemNumber
            self.rotationTarget = - self.arc * (itemNumber / float(len(self.items) - 1))
            self.rotationSteps = sinInterpolation(self.rotation, self.rotationTarget, 45)

        elif itemNumber == 4:
            itemNumber = 0
            self.selectedItem.deselect()
            self.selectedItem = self.items[itemNumber]
            self.selectedItem.select()
            
            self.selectedItemNumber = itemNumber
            self.rotationTarget = - self.arc * (itemNumber / float(len(self.items) - 1))
            self.rotationSteps = sinInterpolation(self.rotation, self.rotationTarget, 45)
        
        elif itemNumber in [-1, -2, -3]:
            itemNumber = itemNumber - (itemNumber * 2)
            if itemNumber == 1:
                itemNumber = 3
            elif itemNumber == 3:
                itemNumber = 1

            self.selectedItem.deselect()
            self.selectedItem = self.items[itemNumber]
            self.selectedItem.select()
            
            self.selectedItemNumber = itemNumber
            self.rotationTarget = - self.arc * (itemNumber / float(len(self.items) - 1))
            self.rotationSteps = sinInterpolation(self.rotation, self.rotationTarget, 45)
        return itemNumber
    
    def rotate(self, angle):
        for i in range(len(self.items)):
            item = self.items[i]
            n = i / float(len(self.items) - 1)
            rot = self.defaultAngle + angle + self.arc * n
            
            item.x = self.x + cos(rot) * self.radius
            item.y = self.y + sin(rot) * self.radius
    
    def update(self):
        if len(self.rotationSteps) > 0:
            self.rotation = self.rotationSteps.pop(0)
            self.rotate(self.rotation)
    
    def draw(self, display):
        for item in self.items:
            item.draw(display)

class MenuItem:
    def __init__(self, text="Spam"):
        self.text = text
        
        self.defaultColor = (255,255,255)
        self.selectedColor = (94,115,210)
        self.color = self.defaultColor
        
        self.x = 0
        self.y = 0 #The menu will edit these
        
        self.font = pygame.font.Font(None, 20)
        self.image = self.font.render(self.text, True, self.color)
        size = self.font.size(self.text)
        self.xOffset = size[0] / 2
        self.yOffset = size[1] / 2
    
    def select(self):
        self.color = self.selectedColor
        self.redrawText()
    
    def deselect(self):
        self.color = self.defaultColor
        self.redrawText()
    
    def redrawText(self):
        self.font = pygame.font.Font(None, 20)
        self.image = self.font.render(self.text, True, self.color)
        size = self.font.size(self.text)
        self.xOffset = size[0] / 2
        self.yOffset = size[1] / 2
    
    def draw(self, display):
        display.blit(self.image, (self.x-self.xOffset, self.y-self.yOffset))

def Opcoes():
    global altura, largura, bombas, altura_tela, largura_tela, com_som
    screen_width, screen_height = 640, 480
    
    screen = pygame.display.set_mode((screen_width,screen_height), 0, 32)
    
    exit_buttons = [pygame.image.load("images" + os.sep + "back" + str(i+1) + ".png") for i in xrange(2)]
    exit_button = exit_buttons[0]
    exit_size = exit_button.get_size()
    exit_pos = (575, 440)

    fonte = pygame.font.SysFont("courrier new", 32, bold = False)
    str_som = fonte.render("Sound", True, [255,255,255])

    facil_buttons = [pygame.image.load("images" + os.sep + "facil" + str(i+1) + ".png") for i in xrange(2)]
    medio_buttons = [pygame.image.load("images" + os.sep + "medio" + str(i+1) + ".png") for i in xrange(2)]
    dificil_buttons = [pygame.image.load("images" + os.sep + "dificil" + str(i+1) + ".png") for i in xrange(2)]
    sound_buttons = [pygame.image.load("images" + os.sep + "sound" + str(i+1) + ".png") for i in xrange(2)]

    facil_button = facil_buttons[0]
    medio_button = medio_buttons[0]
    dificil_button = dificil_buttons[0]
    sound_button = sound_buttons[0]
    
    facil_size = facil_button.get_size()
    medio_size = medio_button.get_size()
    dificil_size = dificil_button.get_size()
    sound_size = sound_button.get_size()
    
    facil_pos = (380,20)
    medio_pos = (370,170)
    dificil_pos = (365,320)
    sound_pos = (0,395)

    background = pygame.image.load("images" + os.sep + "opcoes.jpg").convert()

    pressed = False
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
                    

        mouse_pos = pygame.mouse.get_pos()
        mouse_press = pygame.mouse.get_pressed()

        # Sair
        if exit_pos[0] <= mouse_pos[0] <= exit_pos[0] + exit_size[0] and exit_pos[1] <= mouse_pos[1] <= exit_pos[1] + exit_size[1]:
            exit_button = exit_buttons[1]

            if mouse_press[0]:
                exit_button = exit_buttons[1]
                pressed = True

            if pressed and not mouse_press[0]:
                exit_button = exit_buttons[0]
                return

        else: exit_button = exit_buttons[0]


        #sound
        if sound_pos[0] <= mouse_pos[0] <= sound_pos[0] + sound_size[0] and sound_pos[1] <= mouse_pos[1] <= sound_pos[1] + sound_size[1]:
            sound_button = sound_buttons[1]

            if mouse_press[0] and com_som == 1:
                sound_button = sound_buttons[0]
                pygame.mixer.music.play(-1)
                com_som = 0

            elif mouse_press[0] and com_som == 0:
                sound_button = sound_buttons[1]
                pygame.mixer.music.stop()
                com_som = 1
            
        
            if pressed and not mouse_press[0]:
                sound_button = sound_buttons[0]
                return

        else: sound_button = sound_buttons[0]

        

        # Facil
        if facil_pos[0] <= mouse_pos[0] <= facil_pos[0] + facil_size[0] and facil_pos[1] <= mouse_pos[1] <= facil_pos[1] + facil_size[1]:
            facil_button = facil_buttons[1]

            if mouse_press[0]:
                facil_button = facil_buttons[1]
                altura = 8
                largura = 8
                bombas = 10
                altura_tela = 500
                largura_tela = 260
                pressed = True

            if pressed and not mouse_press[0]:
                facil_button = facil_buttons[0]
                return

        else: facil_button = facil_buttons[0]

        # Medio
        if medio_pos[0] <= mouse_pos[0] <= medio_pos[0] + medio_size[0] and medio_pos[1] <= mouse_pos[1] <= medio_pos[1] + medio_size[1]:
            medio_button = medio_buttons[1]

            if mouse_press[0]:
                medio_button = medio_buttons[1]
                pressed = True

            if pressed and not mouse_press[0]:
                medio_button = medio_buttons[0]
                altura = 16
                largura = 16
                bombas = 40
                altura_tela = 770
                largura_tela = 515
                return

        else: medio_button = medio_buttons[0]

        # Dificil
        if dificil_pos[0] <= mouse_pos[0] <= dificil_pos[0] + dificil_size[0] and dificil_pos[1] <= mouse_pos[1] <= dificil_pos[1] + dificil_size[1]:
            dificil_button = dificil_buttons[1]

            if mouse_press[0]:
                dificil_button = dificil_buttons[1]
                pressed = True

            if pressed and not mouse_press[0]:
                dificil_button = dificil_buttons[0]
                altura = 16
                largura = 21
                bombas = 99
                altura_tela = 920
                largura_tela = 515
                return

        else: dificil_button = dificil_buttons[0]

        if not mouse_press[0]:
            pressed = False

        screen.blit(background, (0,0))
        screen.blit(exit_button, exit_pos)
        screen.blit(facil_button, facil_pos)
        screen.blit(medio_button, medio_pos)
        screen.blit(dificil_button, dificil_pos)
        if com_som == 1:
            screen.blit(sound_buttons[1], sound_pos)
        else:
            screen.blit(sound_buttons[0], sound_pos)
            
        screen.blit(str_som,(20,370))
        
        pygame.display.update()

def creditos():
    screen_width, screen_height = 640, 480

    screen = pygame.display.set_mode((screen_width,screen_height), 0, 32)

    exit_buttons = [pygame.image.load("images" + os.sep + "back" + str(i+1) + ".png") for i in xrange(2)]
    exit_button = exit_buttons[0]
    exit_size = exit_button.get_size()
    exit_pos = (575, 440)

    background = pygame.image.load("images" + os.sep + "creditos.jpg").convert()

    pressed = False
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()

        mouse_pos = pygame.mouse.get_pos()
        mouse_press = pygame.mouse.get_pressed()

        # Sair
        if exit_pos[0] <= mouse_pos[0] <= exit_pos[0] + exit_size[0] and exit_pos[1] <= mouse_pos[1] <= exit_pos[1] + exit_size[1]:
            exit_button = exit_buttons[1]

            if mouse_press[0]:
                exit_button = exit_buttons[1]
                pressed = True

            if pressed and not mouse_press[0]:
                exit_button = exit_buttons[0]
                return

        else: exit_button = exit_buttons[0]

        if not mouse_press[0]:
            pressed = False

        screen.blit(background, (0,0))
        screen.blit(exit_button, exit_pos)
        pygame.display.update()


def logo_inicial():
    display = pygame.display.set_mode((displayWidth, displayHeight))
    logo = pygame.image.load("images" + os.sep + "logo.jpg")
    
    tempo = 0
    while tempo < 200:
        tempo += 1
        display.blit(logo, (0,0))
        pygame.display.update()
        
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                sys.exit()        
        

def main():
    pygame.init()
    
    display = pygame.display.set_mode((displayWidth, displayHeight))
    image = pygame.image.load("images" + os.sep + "menu.jpg")
    clock = pygame.time.Clock()

    menu = RotatingMenu(320, 240, 220, 4.81, (pi/2.0))

    for i in ["Novo Jogo", "Opcoes", "Creditos", "Sair"]:
        menu.addItem(MenuItem(i))
    menu.selectItem(0)
    
    #Loop
    while True:
        #Handle events
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    menu.selectItem(menu.selectedItemNumber + 1)
                if event.key == pygame.K_RIGHT:
                    menu.selectItem(menu.selectedItemNumber - 1)
                if event.key == pygame.K_RETURN:
                    choice = menu.selectedItemNumber
                    if choice == 0:
                        pygame.mixer.music.pause()
                        global altura, largura, bombas, altura_tela, largura_tela
                        if bombas == 99:
                            try:
                                if com_som == 0:
                                    pygame.mixer.music.load("music" + os.sep + "Doom_dificil.mp3")
                                    pygame.mixer.music.play(-1)
                                else:
                                    pygame.mixer.music.stop()
                            except: pass
                        imprime_campo(inicia_jogo(altura, largura, bombas), altura_tela, largura_tela,com_som)
                        if bombas == 99:
                            if com_som == 0:
                                pygame.mixer.music.load("music" + os.sep + "Bon Jovi - Wanted Dead Or Alive.mp3")
                                pygame.mixer.music.play(-1)
                            else:
                                pygame.mixer.music.stop()    
                        pygame.mixer.music.unpause()
                        main()
                    elif choice == 1:
                        Opcoes()
                    elif choice == 2:
                        creditos()
                    elif choice == 3:
                        sys.exit()
        
        #Update stuff
        menu.update()
        
        #Draw stuff
        display.fill((0,0,0))
        display.blit(image, image.get_rect())
        menu.draw(display)
        pygame.display.flip() #Show the updated scene
        clock.tick(fpsLimit) #Wait a little

   
pygame.mixer.init(44100, -16, 2, 2048)
try:
    if com_som == 0:
        pygame.mixer.music.load("music" + os.sep + "Bon Jovi - Wanted Dead Or Alive.mp3")
        pygame.mixer.music.play(-1)
    else:
        pygame.mixer.music.stop()
except: pass

logo_inicial()
main()
